//app.js
const app = getApp()
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var userType = this.globalData.userType
    // 登录
    var that = this
    // 获取用户信息
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.login({
            success: res => {
              // ------ 获取凭证 ------
              var code = res.code;
              if (code) {
                console.log('获取用户登录凭证：' + code);
                // ------ 发送凭证 ------
                wx.request({
                  url: 'https://www.cycs023.cn/f50m-web/api/login',
                  data: {
                    code: code,
                    userType: userType,
                  },
                  method: 'POST',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    that.globalData.confirmSta = res.data.meta.success;
                    console.log(that.globalData.confirmSta)
                    if (res.statusCode == 200 && res.data.meta.success) {
                      console.log("获取到的token为：" + res.data.data.token)
                      getApp().globalData.token = res.data.data.token
                      // that.globalData.openid = res.data
                      //wx.setStorageSync('openid', res.data)
                      var token = res.data.data.token
                      wx.request({
                        url: 'https://www.cycs023.cn/f50m-web/api/riderworks',
                        method: 'GET',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded',
                          'X-SDX-Token': token
                        },
                        success: function (res) {
                          if (res.statusCode == 200 && res.data.meta.success) {
                            console.log(res.data.data.rows)
                            that.globalData.contact = res.data.data.rows[0].phone
                            that.globalData.realName = res.data.data.rows[0].realName
                            that.globalData.apartmentName = res.data.data.rows[0].apartmentName
                            wx.request({
                              url: 'https://www.cycs023.cn/f50m-web/api/dictorys',
                              method: 'GET',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded',
                                'X-SDX-Token': token
                              },
                              data: {
                                pId: 4
                              },
                              success: function (res) {
                                console.log(res);
                                that.globalData.shangbanStatus = res.data.data[0].id;
                                that.globalData.shangbanName = res.data.data[0].name;
                                that.globalData.xiabanStatus = res.data.data[1].id;
                                that.globalData.xiabanName = res.data.data[1].name;
                                console.log("当前上班字典值：" + that.globalData.shangbanStatus);
                                console.log("当前下班字典值：" + that.globalData.xiabanStatus)
                                if (res.statusCode == 200) {
                                  wx.request({
                                    url: 'https://www.cycs023.cn/f50m-web/api/dictorys',
                                    method: 'GET',
                                    header: {
                                      'content-type': 'application/x-www-form-urlencoded',
                                      'X-SDX-Token': token
                                    },
                                    data: {
                                      pId: 7
                                    },
                                    success: function (res) {
                                      console.log(res);
                                      that.globalData.waitChu = res.data.data[2].id;
                                      that.globalData.zhengChu = res.data.data[3].id;
                                      that.globalData.peisongIng = res.data.data[4].id;
                                      that.globalData.peisongCompleted = res.data.data[5].id;
                                      if (res.statusCode == 200) {
                                        wx.request({
                                          url: 'https://www.cycs023.cn/f50m-web/api/dictorys',
                                          method: 'GET',
                                          header: {
                                            'content-type': 'application/x-www-form-urlencoded',
                                            'X-SDX-Token': token
                                          },
                                          data: {
                                            pId: 13
                                          },
                                          success: function (res) {
                                            console.log(res);
                                            that.globalData.notify = res.data.data[0].id;
                                            that.globalData.notifyName = res.data.data[0].name;
                                          }
                                        })
                                      }
                                    }
                                  })
                                }
                              }
                            })
                            if (res.data.data.rows[0].workStatus == that.globalData.shangbanStatus) {
                              that.globalData.workStatus = res.data.data.rows[0].workStatus;
                              that.globalData.workStatusName = '上班'
                            } else {
                              that.globalData.workStatus = res.data.data.rows[0].workStatus;
                              that.globalData.workStatusName = '下班'
                            }
                          }
                        }
                      })
                    } else{
                      console.log(res.errMsg)
                    }
                  },
                })
              } else {
                console.log('获取用户登录失败：' + res.errMsg);
              }
            }
          })
        }
      }
    })
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        this.globalData.userInfo = res.userInfo

        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
      }
    })

  },

  onReady: function () {

  },
  globalData: {
    waitChu:'14',
    zhengChu:'',
    peisongIng: '16',
    peisongCompleted: '',
    notify: '',
    notifyName: '',
    waitRenling: '13',
    apartmentName:'',
    realName:'',
    contact:'',
    confirmSta:'',

    shangbanStatus: '',
    shangbanName: '',
    xiabanStatus: '',
    xiabanName: '',

    userType: '2',

    userInfo: null,
    token: '',
    workStatus: '',
    workStatusName: '',
    orderId: '',
    formIds: {}
  }
})